package com.ruoyi.web.controller.performance;

import com.etoppaas.performance.domain.PerTemplateSpecRelation;
import com.etoppaas.performance.service.IPerTemplateSpecRelationService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.alibaba.fastjson.JSON;

import java.util.List;

/**
 *  信息操作处理
 * 
 * @author ruoyi
 * @date 2019-06-05
 */
@Controller
@RequestMapping("/performance/perTemplateSpecRelation")
public class PerTemplateSpecRelationController extends BaseController
{
    private String prefix = "performance/perTemplateSpecRelation";
	
	@Autowired
	private IPerTemplateSpecRelationService perTemplateSpecRelationService;
	
	@GetMapping()
	public String perTemplateSpecRelation()
	{
	    return prefix + "/perTemplateSpecRelation";
	}
	
	/**
	 * 查询列表
	 */
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(PerTemplateSpecRelation perTemplateSpecRelation)
	{
		startPage();
        List<PerTemplateSpecRelation> list = perTemplateSpecRelationService.selectPerTemplateSpecRelationList(perTemplateSpecRelation);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出列表
	 */
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(PerTemplateSpecRelation perTemplateSpecRelation)
    {
    	List<PerTemplateSpecRelation> list = perTemplateSpecRelationService.selectPerTemplateSpecRelationList(perTemplateSpecRelation);
        ExcelUtil<PerTemplateSpecRelation> util = new ExcelUtil<PerTemplateSpecRelation>(PerTemplateSpecRelation.class);
        return util.exportExcel(list, "perTemplateSpecRelation");
    }
	
	/**
	 * 新增
	 */
	@GetMapping("/add")
	public String add(PerTemplateSpecRelation perTemplateSpecRelation, ModelMap mmap)
	{
		mmap.put("templateId",perTemplateSpecRelation.getTemplateId());
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存
	 */
	@Log(title = "", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(PerTemplateSpecRelation perTemplateSpecRelation)
	{		
		return toAjax(perTemplateSpecRelationService.insertPerTemplateSpecRelation(perTemplateSpecRelation));
	}

	/**
	 * 修改
	 */
	@GetMapping("/edit/{relationId}")
	public String edit(@PathVariable("relationId") Long relationId, ModelMap mmap)
	{
		PerTemplateSpecRelation perTemplateSpecRelation = perTemplateSpecRelationService.selectPerTemplateSpecRelationById(relationId);
		mmap.put("perTemplateSpecRelation", perTemplateSpecRelation);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存
	 */
	@Log(title = "", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(PerTemplateSpecRelation perTemplateSpecRelation)
	{		
		return toAjax(perTemplateSpecRelationService.updatePerTemplateSpecRelation(perTemplateSpecRelation));
	}
	
	/**
	 * 删除
	 */
	@Log(title = "", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(perTemplateSpecRelationService.deletePerTemplateSpecRelationByIds(ids));
	}

	/**
	 * 修改权重
	 */
	@Log(title = "修改权重", businessType = BusinessType.UPDATE)
	@PostMapping("/updateWeight")
	@ResponseBody
	public AjaxResult updateWeight( String relations)
	{
		List<PerTemplateSpecRelation> relationList=null;
		if(StringUtils.isNotEmpty(relations)){
			relationList= JSON.parseArray(relations,PerTemplateSpecRelation.class);
		}
		if(CollectionUtils.isEmpty(relationList)){
			return new AjaxResult();
		}
		return toAjax(perTemplateSpecRelationService.updateWeight(relationList));
	}

	/**
	 * 校验绩效考核项是否重复
	 */
	@Log(title = "校验绩效考核项是否重复", businessType = BusinessType.OTHER)
	@PostMapping("/checkIsLegal")
	@ResponseBody
	public String checkIsLegal( PerTemplateSpecRelation relation)
	{

		return perTemplateSpecRelationService.checkIsLegal(relation);
	}

}
